package com.garlic.face.jlf.ui;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.transaction.InsertSQLService;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.ServiceInterface;
import com.garlic.face.jlf.transaction.SocketService;
import com.garlic.face.jlf.transaction.balance.ReadSerialPort;
import com.garlic.face.jlf.transaction.balance.SerialPortFunction;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;


public class MainActivity extends BaseActivity implements ReadSerialPort.DataWeight {
    private ImageView mShopBrand;
    private String mBarCode = "";
    private ImageView mArrow;
    private ObjectAnimator mObjectAnimator;
    private long lastClickTime = 0;
    private int current_index = 0;
    private static final String TAG = "MainActivity";
    Button mManualPay;
    private static SocketService mSocketService;
    private Intent insertSQLServiceIntent;
    private Dialog mInsertSQLDialog;
    private Dialog dialog = null;
    private Button memberLoginBtn;
    private final int IS_MEMBER = 2;
    private boolean isMemberLogining = false;
    private ShowHandInputDialog memberLoginDialog;
    private Button chineseLanguage;
    private Button englishLanguage;
    private SerialPortFunction serialPortFunction;
    private String mainActivityBalanceWeight = "0.000";
    private ImageView debug_img;
    private SuperVisorConfirmDialog supervisorLoginDialog;

    /*
    private Handler handler = new Handler();

    private Runnable task =new Runnable() {
        public void run() {
            // TODOAuto-generated method stub
            App.playSound(getResources().getString(R.string.sound_start_scan_tip));
            handler.postDelayed(this,60*1000);//设置延迟时间，此处是5秒
            //需要执行的代码
        }
    };
    */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        debug_img = findViewById(R.id.debug_img);
        findViewById(R.id.debug_image_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.playSound(getResources().getString(R.string.sound_start_scan_tip));
            }
        });
        findViewById(R.id.img_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.playSound(getResources().getString(R.string.sound_start_scan_tip));
            }
        });
        mShopBrand = (ImageView) findViewById(R.id.left);
        mShopBrand.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((System.currentTimeMillis() - lastClickTime) < 500) {
                    current_index++;
                    lastClickTime = System.currentTimeMillis();
                    if (current_index >= 2) {
                        Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                        startActivity(intent);
                    }
                } else {
                    lastClickTime = System.currentTimeMillis();
                    current_index = 0;
                }
            }
        });
        mArrow = (ImageView) findViewById(R.id.arrow);
        mManualPay = findViewById(R.id.manual_pay);
        mManualPay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (Means.getInstance().isLoginServer()) {
                    LogUtils.d("主页点击手动结账时称实时重量:" + Double.parseDouble(mainActivityBalanceWeight), true);
                    if (Double.parseDouble(mainActivityBalanceWeight) > 0.005) {
                        ToastUtils.toastShow(getResources().getString(R.string.tv_empty_goods), ToastUtils.ERROR);
                        App.playSound(getResources().getString(R.string.sound_e3));
                        return;
                    }else if(Double.parseDouble(mainActivityBalanceWeight) < -0.005){
                        ToastUtils.toastShow(getResources().getString(R.string.tv_zero_scale), ToastUtils.ERROR);
                        return;
                    }
                    Intent intent = new Intent(MainActivity.this, ScanGoodsActivity.class);
                    intent.putExtra("handinput", true);
                    startActivity(intent);
                } else {
                    ToastUtils.toastShow("收银员未登录，请联系工作人员", ToastUtils.ERROR);
                }
            }
        });
        startPropertyAnim();
        bindService();
        if (Tools.getNeedInsertSQL()) {
            mInsertSQLDialog = new AlertDialog.Builder(this)
                    .setTitle("注意").setMessage("正在初始化商品重量库，请勿退出应用").setCancelable(false).create();
            mInsertSQLDialog.show();
            insertSQLServiceIntent = new Intent(MainActivity.this, InsertSQLService.class);
            startService(insertSQLServiceIntent);
        }
        memberLoginBtn = findViewById(R.id.member_login);
        memberLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Means.getInstance().isLoginServer()) {
                    LogUtils.d("主页点击会员登录时称实时重量:" + Double.parseDouble(mainActivityBalanceWeight), true);
                    if (Double.parseDouble(mainActivityBalanceWeight) > 0.005) {
                        ToastUtils.toastShow(getResources().getString(R.string.tv_empty_goods), ToastUtils.ERROR);
                        App.playSound(getResources().getString(R.string.sound_e3));
                        return;
                    }else if(Double.parseDouble(mainActivityBalanceWeight) < -0.005){
                        ToastUtils.toastShow(getResources().getString(R.string.tv_zero_scale), ToastUtils.ERROR);
                        return;
                    }
                    showHandInputDialog(IS_MEMBER);
                } else {
                    ToastUtils.toastShow("收银员未登录，请联系工作人员", ToastUtils.ERROR);
                }
            }
        });

        initLanguage();
        doCashierloginTip();
        serialPortFunction = SerialPortFunction.getInstance();
    }
    private void initBalance() {
        serialPortFunction.setContext(this);
        serialPortFunction.startSerialReadEle(this);
    }

    private void initLanguage() {
        chineseLanguage = findViewById(R.id.chinese);
        englishLanguage = findViewById(R.id.en);
        if(Tools.getLanguage().equals(Locale.US.toString())){
            chineseLanguage.setBackgroundResource(R.drawable.border_line_blue);
            chineseLanguage.setTextColor(getResources().getColor(R.color.border_line_blue));
            englishLanguage.setBackgroundResource(R.drawable.btn_bg_blue);
            englishLanguage.setTextColor(getResources().getColor(R.color.white));
            debug_img.setImageResource(R.mipmap.home_down_bg_en);
        }else {
            chineseLanguage.setBackgroundResource(R.drawable.btn_bg_blue);
            chineseLanguage.setTextColor(getResources().getColor(R.color.white));
            englishLanguage.setBackgroundResource(R.drawable.border_line_blue);
            englishLanguage.setTextColor(getResources().getColor(R.color.border_line_blue));
            debug_img.setImageResource(R.mipmap.home_down_bg);
        }
        chineseLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switchLanguage(1);
                chineseLanguage.setBackgroundResource(R.drawable.btn_bg_blue);
                chineseLanguage.setTextColor(getResources().getColor(R.color.white));
                englishLanguage.setBackgroundResource(R.drawable.border_line_blue);
                englishLanguage.setTextColor(getResources().getColor(R.color.border_line_blue));
            }
        });
        englishLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switchLanguage(2);
                chineseLanguage.setBackgroundResource(R.drawable.border_line_blue);
                chineseLanguage.setTextColor(getResources().getColor(R.color.border_line_blue));
                englishLanguage.setBackgroundResource(R.drawable.btn_bg_blue);
                englishLanguage.setTextColor(getResources().getColor(R.color.white));
            }
        });
    }

    private void doCashierloginTip(){
        View root = View.inflate(MainActivity.this, R.layout.cashier_login_tip_dialog, null);
        TextView title = (TextView) root.findViewById(R.id.title_name);
        title.setText("收银员未登录");

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setView(root)
                    .setCancelable(false);
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        root.findViewById(R.id.done).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CashierLoginActivity.class));
            }
        });
    }

    // 动画实际执行
    private void startPropertyAnim() {
        // X轴方向上的坐标
        float translationY = mArrow.getTranslationY();

        // 向右移动500pix，然后再移动到原来的位置复原。
        // 参数“translationX”指明在x坐标轴位移，即水平位移。
        mObjectAnimator = ObjectAnimator.ofFloat(mArrow, "translationY", translationY, 50f,
                translationY);

        mObjectAnimator.setDuration(2000);
        mObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);

        // 回调监听，可以有也可以无。
        // 根据情况，如果需要监听动画执行到何种“进度”，那么就监听之。
        mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float) animation.getAnimatedValue();
//                Log.d("zhangphil", value + "");
            }
        });

        // 正式开始启动执行动画
        mObjectAnimator.start();
    }

 /*   public boolean onKeyUp(int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                if (TextUtils.isEmpty(mBarCode)) {
                    ToastUtils.toastShow( "条码识别错误,请重试", ToastUtils.ERROR);
                    mBarCode = "";
                } else {
                    if (Means.getInstance().isLoginServer()) {
                        Intent intent = new Intent(MainActivity.this, ScanGoodsActivity.class);
                        intent.putExtra("bar_code", mBarCode);
                        startActivity(intent);
                    } else {
                        ToastUtils.toastShow("收银员未登录，请联系工作人员", ToastUtils.ERROR);
                    }
                    mBarCode = "";
                }
            } else if (event.getUnicodeChar() != 0) {
                mBarCode += Tools.asciiToString(String.valueOf(event.getUnicodeChar()));
            }
        return super.onKeyUp(keyCode, event);
    }*/

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            LogUtils.d("getAction=" + String.valueOf(event.getAction())
                    + ";getKeyCode=" + String.valueOf(event.getKeyCode())
                    + ";getCharacters=" + event.getCharacters()
                    + ";getScanCode=" + event.getScanCode()
                    + ";getUnicodeChar=" + event.getUnicodeChar()
                    + ";getNumber=" + event.getNumber(), false);
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                LogUtils.d("dispatchKeyEvent: KEYCODE_ENTER:", false);
                LogUtils.d("Main=" + mBarCode, false);
                if (Means.getInstance().isLoginServer()) {
                    LogUtils.d("主页扫商品ENTER实时重量:" + Double.parseDouble(mainActivityBalanceWeight), true);
                    if(Double.parseDouble(mainActivityBalanceWeight) > 0.005){
                        ToastUtils.toastShow(getResources().getString(R.string.tv_empty_goods), ToastUtils.ERROR);
                        App.playSound(getResources().getString(R.string.sound_e3));
                    }else if(Double.parseDouble(mainActivityBalanceWeight) < -0.005){
                        ToastUtils.toastShow(getResources().getString(R.string.tv_zero_scale), ToastUtils.ERROR);
                    }else {
                        Intent intent = new Intent(MainActivity.this, ScanGoodsActivity.class);
                        intent.putExtra("bar_code", mBarCode);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.toastShow("收银员未登录，请联系工作人员", ToastUtils.ERROR);
                }
                mBarCode = "";
            } else if (event.getUnicodeChar() == 0) {
            } else if ((char) event.getUnicodeChar() == '#') {
                if (mBarCode.startsWith("null")) {
                    mBarCode = mBarCode.replace("null", "");
                }
                LogUtils.d("主页扫商品收银员是否登录:" + Means.getInstance().isLoginServer() +" mBarCode:" + mBarCode, true);
                if (Means.getInstance().isLoginServer()) {
                        LogUtils.d("主页扫商品#实时重量:" + Double.parseDouble(mainActivityBalanceWeight), true);
                        if(Double.parseDouble(mainActivityBalanceWeight) > 0.005){
                            ToastUtils.toastShow(getResources().getString(R.string.tv_empty_goods), ToastUtils.ERROR);
                            App.playSound(getResources().getString(R.string.sound_e3));
                        }else if(Double.parseDouble(mainActivityBalanceWeight) < -0.005){
                            ToastUtils.toastShow(getResources().getString(R.string.tv_zero_scale), ToastUtils.ERROR);
                        }else if(mBarCode.substring(1, mBarCode.length()).startsWith("88")){
                            ToastUtils.toastShow(getResources().getString(R.string.error_input), ToastUtils.ERROR);
                        }else {
                            Intent intent = new Intent(MainActivity.this, ScanGoodsActivity.class);
                            intent.putExtra("bar_code", mBarCode);
                            startActivity(intent);
                        }
                    } else {
                        ToastUtils.toastShow("收银员未登录，请联系工作人员", ToastUtils.ERROR);
                    }
                mBarCode = "";
            } else {
                mBarCode += (char) event.getUnicodeChar();
                Log.d(TAG, "Main=" + mBarCode.toString());
            }
        }

        return super.dispatchKeyEvent(event);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //回到主页代表开始新的交易，清除之前的信息
        Means.getInstance().clear();
        Means.TotalWeight = 0;
        mainActivityBalanceWeight = "0.000";
        if(Means.getInstance().isLoginServer()){
            if(dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }
        initBalance();
        App.playSound(getResources().getString(R.string.sound_start_scan_tip));
//        handler.postDelayed(task,2000);//延迟调用
    }

    @Override
    protected void onPause() {
        super.onPause();
        serialPortFunction.closeEle();
        App.getSoundMgr().stopPlay();
//        handler.removeCallbacks(task);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //TODO:切换语言的时候.这块代码有问题..暂时先这样.看看会不会影响其他问题..
//        EventBus.getDefault().unregister(this);
//        unBindService();
//        mSocketService = null;
//        LogUtil.d("xxxx socketService is null");
//        App.getSoundMgr().release();
    }


    protected void bindService() {
        Intent intent = new Intent(this, SocketService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    protected void unBindService() {
        unbindService(conn);
    }

    private ServiceConnection conn = new ServiceConnection() {
        /** 获取服务对象时的操作 */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mSocketService = ((SocketService.ServiceBinder) service).getService();
        }

        /** 无法获取到服务对象时的操作 */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mSocketService = null;
        }

    };

    public static SocketService getSocketService() {
        return mSocketService;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveResponse(String string) {
        LogUtils.d("接收到配置完成:" + string, true);
        if (ConfigActivity.CONFIG_DONE.equals(string)) {
            mSocketService.initService();
            LogUtils.d(" 再次初始化 service", true);
        }else if(SocketService.BACK_TO_MAIN.equals(string)){
            LogUtils.d("清除上层页面回到主页", true);
            App.clearToMainActivity();
        }else if(InsertSQLService.INSERT_GOODSWEIGHT_TABLE_FAILED.equals(string) || InsertSQLService.INSERT_ITEMEXCEPTIONS_TABLE_FAILED.equals(string)){
            LogUtils.e("INSERT_SQL_FAILED");
            Tools.setNeedInsertSQL(true);
            mInsertSQLDialog.dismiss();
        }else if(InsertSQLService.INSERT_ITEMEXCEPTIONS_TABLE_SUCCESS.equals(string) && InsertSQLService.INSERT_ITEMEXCEPTIONS_TABLE_SUCCESS.equals(string)){
            LogUtils.e("INSERT_SQL_SUCCESS");
            Tools.setNeedInsertSQL(false);
            if(mInsertSQLDialog != null){
                mInsertSQLDialog.dismiss();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveServiceResponse(JSONObject jsonObject) {
        if(!App.isMainActivity()){
            return;
        }
        LogUtils.d("receive msg" + jsonObject, true);
        try {
            String interfaceType = jsonObject.getString("INTERFACETYPE");
            String retflag = jsonObject.getString("retflag");
            String retmsg = jsonObject.getString("retmsg");

            if (interfaceType.equals(JLFConstant.TAG_STATE_RESPONSE)) {
                //状态
                if (retflag.equals("0")) {
                    if (retmsg.equals(JLFConstant.POS_CLOSE)) {
                        if(dialog != null && !dialog.isShowing()){
                            dialog.show();
                        }
                    }else if (retmsg.equals(JLFConstant.POS_OPEN)) {
                        if(dialog != null && dialog.isShowing()){
                            dialog.dismiss();
                        }
                        Means.getInstance().setLoginServer(true);
                    } else if (retmsg.equals(JLFConstant.POS_BETWEEN_ITEM)) {
//                        ServiceInterface.getInstance().cancelTrade("91138005", "3");
                        ServiceInterface.getInstance().cancelTrade("", "");
                    } else if (retmsg.equals(JLFConstant.POS_SUBTOTAL)) {
//                        ServiceInterface.getInstance().cancelTrade("91138005", "3");
                        ServiceInterface.getInstance().cancelTrade("", "");
                    } else if (retmsg.equals(JLFConstant.POS_PARTIAL_PAYMENT)) {
                        ToastUtils.toastShow("部分支付,需要重启POS", ToastUtils.ERROR);
                        showSuperLoginDialog();
                    } else {
                        ToastUtils.toastShow("POS状态异常,需要重启POS", ToastUtils.ERROR);
                    }
                } else {
                    ToastUtils.toastShow("POS状态异常,需要重启POS", ToastUtils.ERROR);
                }
            } else if (interfaceType.equals(JLFConstant.TAG_CANCELTRADE_RESPONSE)) {
                hideProgress();
                if (retflag.equals("0")) {
                    Tools.clearTrade();
                    Means.getInstance().setLoginServer(true);
                } else {
                    LogUtils.d("主页取消交易:" + retmsg, true);
                }
            } else if (interfaceType.equals(JLFConstant.TAG_VIP_RESPONSE)) {
                hideProgress();
                if(!memberLoginDialog.isShowing()){
                    return;
                }
                memberLoginDialog.dismiss();
                isMemberLogining = false;
                if (retflag.equals("0")) {
                    String cardnumber = jsonObject.getString("cardnumber");
                    String points = jsonObject.getString("points");
                    Means.getInstance().getRec_Tran().setCustcode(cardnumber);
                    Means.getInstance().getRec_Tran().setPoints(points);
                    ToastUtils.toastShow(getResources().getString(R.string.vip_login_success), ToastUtils.SUCCESS);
                    Intent intent = new Intent(MainActivity.this, ScanGoodsActivity.class);
                    intent.putExtra("cardnumber", cardnumber);
                    intent.putExtra("points", points);
                    startActivity(intent);
                } else {
                    ToastUtils.toastShow(getResources().getString(R.string.vip_login_failed)+ ":" + retmsg, ToastUtils.ERROR);
                    LogUtils.e("会员登录失败:" + retmsg);
                }
            }else if (interfaceType.equals(JLFConstant.TAG_SUPERVISOR_RESPONSE)) {
                retflag = jsonObject.getString("retflag");
                retmsg = jsonObject.getString("retmsg");
                //TODO 刷卡输完管理员账号密码返回可能retflag为1，return retmsg为100000
                if (retflag.equals("0")) {
                    if (jsonObject.has("receiptLines")){
                        String receiptLines = jsonObject.getString("receiptLines");
                        if(!TextUtils.isEmpty(receiptLines)) {
                            String[] receiptArray = receiptLines.replace("[", "").replace("]", "").split(",");
                            ArrayList<String> receiptList = new ArrayList<String>();
                            for (int i = 0; i < receiptArray.length; i++) {
                                receiptList.add(receiptArray[i]);
                            }
                            Means.getInstance().setPrintBillHead(receiptList);
                        }
                    }
//                    mView.printText();
                    ServiceInterface.getInstance().cancelTrade("", "");
                } else {
                    ServiceInterface.getInstance().cancelTrade("", "");
                    ToastUtils.toastShow(retmsg, ToastUtils.ERROR);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void showHandInputDialog(final int type) {
        isMemberLogining = true;
        memberLoginDialog = new ShowHandInputDialog(this, type);
        memberLoginDialog.setCallBack(new ShowHandInputDialog.CallBack() {
            @Override
            public void close(View view) {
                isMemberLogining = false;
                memberLoginDialog.dismiss();
            }

            @Override
            public void done(String code) {
                getMemberInfo(code);
            }
        });
        memberLoginDialog.show();
    }


    private void getMemberInfo(String vipNumber) {
        showProgress();
        ServiceInterface.getInstance().getVipNumber(vipNumber);
    }


    private void switchLanguage(int index) {
        Locale locale = null;
        switch (index) {
            case 1:
                locale = Locale.CHINA;
                break;
            case 2:
                locale = Locale.US;
                break;
        }

        App.getInstances().updateLocale(locale);

        String language = "auto";
        if (locale != null) {
            language = locale.toString();
        }
        App.getInstances().saveLanguage(language);
        unBindService();
        mSocketService = null;
        Intent intent = new Intent(this, MainActivity.class);
        //开始新的activity同时移除之前所有的activity
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void weight(String weight) {
        if (Means.blAssistMode || !Means.blScaleOK) {
            mainActivityBalanceWeight = "0.000";
            return;
        }
        mainActivityBalanceWeight = weight;
    }

    private void showSuperLoginDialog() {
        supervisorLoginDialog = new SuperVisorConfirmDialog(this);
        supervisorLoginDialog.setCanceledOnTouchOutside(false);
        supervisorLoginDialog.setCancelable(false);
        supervisorLoginDialog.setCallBack(new SuperVisorConfirmDialog.CallBack() {
            @Override
            public void close(View view) {
//                supervisorLoginDialog.dismiss();
                ToastUtils.toastShow("请扫描合法的助理员工授权码", ToastUtils.ERROR);
                return;
            }

            @Override
            public void done(String account, String psw) {
                supervisorLoginDialog.dismiss();
                showProgress();
                ServiceInterface.getInstance().superVisor(account, psw, JLFConstant.SCO_PARTIAL_PAY_PSV);
            }

            @Override
            public void finish(String account_psw) {
                LogUtils.d("account_psw msg" + account_psw, true);
                if(TextUtils.isEmpty(account_psw) || !account_psw.contains("&")){
                    ToastUtils.toastShow("请扫描合法的助理员工授权码", ToastUtils.ERROR);
                    return;
                }
                String date = account_psw.substring(0, account_psw.indexOf("&"));
                String account = account_psw.substring(account_psw.indexOf("&") + 1, account_psw.indexOf("-"));
                String psw = account_psw.substring(account_psw.indexOf("-") + 1, account_psw.length());
                supervisorLoginDialog.dismiss();
                if(!checkTimeLegal(date)){
                    ToastUtils.toastShow("二维码已过期", ToastUtils.ERROR);
                }else {
                    showProgress();
                    ServiceInterface.getInstance().superVisor(account, psw, JLFConstant.SCO_PARTIAL_PAY_PSV);
                }
            }
        });
        supervisorLoginDialog.show();
    }

    private boolean checkTimeLegal(String time){
        boolean legal = false;
        long currentTime = new Date().getTime();

        long printTime = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = format.parse(time);
            printTime = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        LogUtils.d("首页判断管理员二维码过期时间:" + "currentTime:" + currentTime + "printTime:" + printTime, true);
        if(((currentTime - printTime) < (24* 3600000))){
            legal = true;
        }

        return legal;
    }
}
